<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">用户列表</h1>
  </el-header>
  <el-main>
    <!-- 查询条件表单 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="queryForm.userName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="queryForm.userTel" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="queryForm.userEmail" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-select v-model="queryForm.userRole" placeholder="权限">
          <el-option label="普通管理员" value="admin"></el-option>
          <el-option label="超级管理员" value="sadmin"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
      </el-form-item>
    </el-form>
    <download-excel
             class="export-excel-wrapper"
             :data="tableAllData"
             :fields="fieldLabels"
             :header="fileName"
             name="用户数据.xls"
           >
             <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
      <el-button type="success" @click="exportExcel">导出</el-button>
    </download-excel>
    <el-table :data="tableData" style="100%" >
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="userId" label="编号" style="0%"></el-table-column>
      <el-table-column prop="userName" label="姓名" style="0%"></el-table-column>
      <el-table-column prop="userEmail" label="邮箱" style="0%"></el-table-column>
      <el-table-column prop="userTel" label="电话" style="0%"></el-table-column>
      <el-table-column prop="userRole" label="权限" style="0%"></el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
    @size-change="select"
    @current-change="selectPage"
    :current-page="queryForm.page"
    :page-sizes="[10,15,20]"
    :page-size="queryForm.row"
    :total="total"
    layout="total,sizes,prev,pager,next,jumper"
    >

    </el-pagination>
  </el-main>
</el-container>
</template>

<script>
  export default {
      data() {
        return {
          tableAllData:[],
          fieldLabels:{
            '编号':'userId',
            '姓名':'userName',
            '邮箱':'userEmail',
            '电话':'userTel',
            '权限':'userRole'
          },
          fileName:"用户数据",
          tableData: [],
          totalPage: 0, //总页数
          total: 0, //总条数
          queryForm: {
            page: 1,
            row: 10,
            userName:"",
            userTel:"",
            userEmail:"",
            userRole:"",
          }
        }
      },
      methods: {
        loadData() { //加载数据
          console.log(this.queryForm)
          var self = this;
          var http = this.$http.post("/api/user/userIndex",this.$qs.stringify(this.queryForm));
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableData = rs.data.data.data;
              self.total = rs.data.data.total;
              self.totalPage = rs.data.data.totalPage;
            }else{
              self.tableData=[];
              self.total=0;
            }
          })
        },
        select(row){ //分页下拉框选中触发的事件，row是当前选中条数
          this.queryForm.row=row;
          //刷新
          this.loadData();
        },
        selectPage(page){//选中页码触发的事件, page就是选中的页码
          this.queryForm.page = page;
          //刷新
          this.loadData();
        },
        exportExcel(){
          var self = this;
          var http = this.$http.post("/api/user/selectAllUser");
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableAllData = rs.data.data
            }
          })
        }
      },
      mounted(){
        this.loadData();
      }
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
